package cn.nsu.douban.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.activiti.rest.api.engine.RestIdentityLink;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.nsu.douban.base.baseModelDao.BaseQueryModel;
import cn.nsu.douban.dao.dao.IMovieDao;
import cn.nsu.douban.entity.MovieInfo;

@Repository
@SuppressWarnings("all")
public class IMovieDaoImpl  extends HibernateDaoSupport implements IMovieDao {
	@Resource
	public void setSesssionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	/**
	 * 得到每页的电影信息
	 */
	@Transactional(readOnly=true)
	public List<MovieInfo> getAll(Integer pageNum, Integer pageSize) {
	DetachedCriteria deCriteria=DetachedCriteria.forClass(MovieInfo.class);
		return this.getHibernateTemplate().findByCriteria(deCriteria, (pageNum-1)*pageSize, pageSize);
	
	}
	
	//得到热门总数
	@Transactional(readOnly=true)
	public Integer AllCount() {
      Class clazz=MovieInfo.class;
	DetachedCriteria detachedCriteria=DetachedCriteria.forClass(clazz);
	detachedCriteria.add(Restrictions.eq("ishow", "1"));
	detachedCriteria.setProjection(Projections.rowCount());
	List<Long> list = this.getHibernateTemplate().findByCriteria(detachedCriteria);
		return list.get(0).intValue();
	}
    //根据ID查询对象
	public MovieInfo getByMovieId(Integer mid) {
	
		return this.getHibernateTemplate().get(MovieInfo.class, mid);
	}
    //ajx
	@Transactional(readOnly=true)
	public List<MovieInfo> getMovieInfoByNmae(String search_text) {
		  List<MovieInfo> mList;
      DetachedCriteria detachedCriteria=DetachedCriteria.forClass(MovieInfo.class);
      if(search_text!=null&&search_text.trim().length()>0){
    	
      detachedCriteria.add(Restrictions.like("name","%"+search_text.trim()+"%" ));
       mList = this.getHibernateTemplate().findByCriteria(detachedCriteria);
      } else{
    	mList = getAll(1, 4);
      }
		return mList;
	}
	
	public List<MovieInfo> findMovieByDate() {
           
		 String hql="from MovieInfo order by date desc";
		   List<MovieInfo> find = this.getHibernateTemplate().find(hql);
		return find;
	}
	
	public List<MovieInfo> getAll() {
		
		return this.getHibernateTemplate().find("from MovieInfo");
	}
	
	public void update(MovieInfo mi) {

		 this.getHibernateTemplate().update(mi);
	}
	
	public MovieInfo getMovieInfoById(Integer id) {
		String hql="from MovieInfo where id=?";
		List<MovieInfo> mList= this.getHibernateTemplate().find(hql, id);
		if(mList!=null&&mList.size()>0){
			return mList.get(0);
		}
		return null;
	}
	public void delete(MovieInfo mInfo) {
    this.getHibernateTemplate().delete(mInfo);	
	}
	@Override
	public void save(MovieInfo mi) {
		  this.getHibernateTemplate().save(mi);
		  
	}
	@Override
	public void deleteMovieByIds(String string) {
		 this.getHibernateTemplate().bulkUpdate(string);
	}

	  
	
}
